#! /usr/bin/env python3
# encoding=utf-8

import os
import logging
from app.config.settings import OUTPUT_CONCURRENCY

from .common import (
    start_workers,
    register_sigterm_handler,
)
from .consume import run

logger = logging.getLogger(__name__)


def main():
    # create new process group to terminate all created subprocess
    try:
        os.setpgrp()
    except Exception:
        pass
    logger.info('start output module')

    register_sigterm_handler(is_cleanup_kill=True)
    kind = 'output'
    mode = 'process'
    concurrency = OUTPUT_CONCURRENCY
    if concurrency == 0:
        logger.info('start in singer worker mode')
        run(kind)
    else:
        logger.info('start {} workers in {} mode'.format(concurrency, mode))
        start_workers(
            name=kind,
            target=run,
            mode=mode,
            concurrency=concurrency,
            is_block=True,
            is_pass_name=True,
            is_kill_group=True,
        )


if __name__ == '__main__':
    main()
